package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import com.heima.pojo.TbRegion;
import com.heima.pojo.dto.RegionDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.RegionVo;
import com.heima.service.ITbRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 运营区域 前端控制器
 * </p>
 *
 * @author nan
 * @since 2024-06-07
 */
@RestController
@RequestMapping("/api/vm-service/region")
@Api(tags = "运营区域管理")
public class TbRegionController {

    @Autowired
    private ITbRegionService regionService;

    @GetMapping("/search")
    @ApiOperation("区域搜索")
    public PageResponse<RegionVo> getByPage(@RequestParam Integer pageIndex, @RequestParam Integer pageSize, @RequestParam(required = false) String name) {

        return regionService.getByPage(pageIndex, pageSize, name);
    }

    @PostMapping
    @ApiOperation("新增区域")
    public Boolean addRegion(@RequestBody RegionDto regionDto) {
        TbRegion tbRegion = BeanUtil.toBean(regionDto, TbRegion.class);
        tbRegion.setName(regionDto.getRegionName());
        return regionService.save(tbRegion);
    }

    @PutMapping("/{regionId}")
    @ApiOperation("修改区域")
    public Boolean updateRegion(@PathVariable String regionId, @RequestBody RegionDto regionDto) {
        TbRegion tbRegion = BeanUtil.toBean(regionDto, TbRegion.class);
        tbRegion.setId(Long.parseLong(regionId));
        tbRegion.setName(regionDto.getRegionName());
        return regionService.updateById(tbRegion);
    }

    @DeleteMapping("/{regionId}")
    @ApiOperation("删除区域")
    public Boolean deleteRegion(@PathVariable String regionId) {
        return regionService.removeById(regionId);
    }
}
